package com.lyn.plugin;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.lyn.plugin.entity.PluginParameter;
import com.lyn.plugin.interfaces.ProxyActivityInterface;
import com.lyn.plugin.utils.LogUtil;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理activity
 */
public class ProxyActivity extends AppCompatActivity implements InvocationHandler {

    public final static String ACTIVITY_CLASS_NAME = "activityClassName";
    public final static String PLUGIN_PATH = "pluginPath";
    private ProxyActivityInterface proxyActivityInterface;
    private Object newInstance;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String activityClassName = intent.getStringExtra(ACTIVITY_CLASS_NAME);
        String pluginPath = intent.getStringExtra(PLUGIN_PATH);
        try {
            Class<?> aClass = Class.forName(activityClassName);
            newInstance = aClass.newInstance();
            proxyActivityInterface = (ProxyActivityInterface) Proxy.newProxyInstance(getClassLoader(), newInstance.getClass().getInterfaces(), this);
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(e.getMessage());
            return;
        }
        Resources resources = PluginHelper.getResources(getApplication(), pluginPath);
        PluginParameter parameter = new PluginParameter();
        parameter.setSavedInstanceState(savedInstanceState);
        parameter.setActivity(this);
        parameter.setResources(resources);
        parameter.setPluginPath(pluginPath);

        proxyActivityInterface.onLifeCycle(getMethodName(), parameter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        proxyActivityInterface.onLifeCycle(getMethodName());
    }

    @Override
    protected void onResume() {
        super.onResume();
        proxyActivityInterface.onLifeCycle(getMethodName());
    }

    @Override
    protected void onStop() {
        super.onStop();
        proxyActivityInterface.onLifeCycle(getMethodName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        proxyActivityInterface.onLifeCycle(getMethodName());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        proxyActivityInterface.onLifeCycle(getMethodName());
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        proxyActivityInterface.onLifeCycle(getMethodName());
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        proxyActivityInterface.onLifeCycle(getMethodName(), requestCode, permissions, grantResults);
    }


    private String getMethodName() {
        return Thread.currentThread().getStackTrace()[3].getMethodName();
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return method.invoke(newInstance, args);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        proxyActivityInterface.onLifeCycle(getMethodName(), requestCode, resultCode, data);
    }
}
